import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
import router from './router';
import '@/assets/iconfont/iconfont.css';
import request from '@/utils/request';
import Common from '@/utils/common';
import axios from 'axios';
import store from '@/store/store';
import SanitizeDirective from '@/utils/sanitize'; // 防止xss输入攻击

Vue.config.productionTip = false;

Vue.use(ElementUI);

//挂载
Vue.prototype.$request = request;
Vue.prototype.$axios = axios;
Vue.prototype.$Common = Common;
Vue.prototype.$store = store;

// 全局节流
const on = Vue.prototype.$on;
Vue.prototype.$on = function (event, func) {
    let previous = 0;
    let newFunc = func;
    if (event === 'click') {
        newFunc = function () {
            const now = new Date().getTime();
            if (previous + 2000 <= now) {
                func.apply(this, arguments);
                previous = now;
            }
        };
    }
    on.call(this, event, newFunc);
};

// 防止xss输入攻击
Vue.directive('sanitize', SanitizeDirective);

new Vue({
    router,
    render: h => h(App),
}).$mount('#app');

// 前端过滤特殊字符
function sanitizeInput(input) {
    console.log(new Date().getTime());
    return input
        .replace(/<script.*?>.*?<\/script>/gi, '') // 移除<script>标签
        .replace(/alert\s*\(.*?\)/gi, '') // 移除alert()调用
        .replace(/<iframe.*?>.*?<\/iframe>/gi, '') // 移除<iframe>标签
        .replace(/on\w+=".*?"/gi, '') // 移除所有内联事件处理程序，如onClick
        .replace(/javascript:/gi, ''); // 禁止javascript:伪协议
}
function sanitizeResponse(response) {
    // 创建新对象，保留data的原始内容
    const sanitizedResponse = {};

    // 遍历原始响应对象的属性
    for (let key in response) {
        if (response.hasOwnProperty(key)) {
            if (key === 'data') {
                // 对data属性，直接保留原始内容，不进行过滤
                sanitizedResponse[key] = response[key];
            } else if (typeof response[key] === 'string') {
                // 对字符串进行过滤
                sanitizedResponse[key] = sanitizeInput(response[key]);
            } else if (typeof response[key] === 'object' && response[key] !== null) {
                // 对象类型，递归处理
                sanitizedResponse[key] = sanitizeResponse(response[key]);
            } else {
                // 对于其他类型的数据，直接复制
                sanitizedResponse[key] = response[key];
            }
        }
    }

    return sanitizedResponse;
}

const mockResponse = {
    data: new Uint8Array([0, 1, 2, 3]).buffer, // 模拟二进制数据
    status: 200,
    statusText: "<script>alert('XSS')</script>", // 需要过滤的内容
    headers: {
        'content-type': 'application/json',
        'x-xss-protection': '1; mode=block',
    },
    config: {
        url: 'api/system/getVerifyCode',
        responseType: 'arraybuffer',
    },
    extra: "<iframe src='evil.com'></iframe>", // 需要过滤的内容
    result: {
        id: 1,
        companyNumber: 'CN-SZ-00-0201',
        pageStyle: '[{"styleType":1,"styleName":"浅灰","status":0},{"styleType":2,"styleName":"深黑","status":1}]',
        reservationFunction:
            '[{"functionName":"视频会议","functionType":1,"status":1},{"functionName":"无纸化会议","functionType":2,"status":1},{"functionName":"是否保密","functionType":3,"status":1},{"functionName":"是否签到","functionType":4,"status":1},{"functionName":"座位编排","functionType":5,"status":1},{"functionName":"附加选项","functionType":6,"status":0},{"functionName":"设备选项","functionType":7,"status":0}]',
        notification:
            '[{"notificationType":1,"notificationName":"短信","status":0},{"notificationType":2,"notificationName":"邮件","status":0},{"notificationType":4,"notificationName":"微信","status":0},{"notificationType":3,"notificationName":"welink","status":1}]',
        loginMethod:
            '[{"methodType":1,"methodName":"短信","status":0},{"methodType":2,"methodName":"微信","status":0},{"methodType":3,"methodName":"welink","status":0},{"methodType":4,"methodName":"钉钉","status":0}]',
        otherFunction: '[{"functionName":"审批流程","functionType":1,"status":1},{"functionName":"支付功能","functionType":2,"status":1}]',
        homeOptions:
            '[{"enterDirectly":1,"optionsName":"会议预定","optionsType":1,"serverLogo":"http://192.168.5.203:443/groupundefined","status":1},{"enterDirectly":0,"optionsName":"会管运维","optionsType":2,"serverLogo":"http://192.168.5.203:443/groupundefined","status":0},{"enterDirectly":0,"optionsName":"信息发布","optionsType":3,"serverLogo":"http://192.168.5.203:443/groupundefined","status":0},{"enterDirectly":0,"optionsName":"视讯系统","optionsType":4,"serverLogo":"http://192.168.5.203:443/groupundefined","status":0}]',
        picture:
            '[{"picturePath":"http://192.168.5.203:443/group1/M00/00/02/wKgFvl7Pjq2AZfn6AAIEhh4GXdo635.png","pictureName":"ED76623A-00B9-41c7-9673-CF947AC358E0.png","pictureType":1,"status":1},{"picturePath":"http://192.168.5.203:443/group1/M00/00/02/wKgFvl7PjwCAUZHqAAIEhh4GXdo292.png","leftMargin":0,"high":0,"topMargin":0,"width":0,"pictureName":"ED76623A-00B9-41c7-9673-CF947AC358E0.png","pictureType":2,"status":1},{"picturePath":"http://192.168.5.203:443/group1/M00/00/02/wKgFvl7PpK2AVnCjAAATsI0x5Xk780.png","pictureName":"logo-a.png","pictureType":3,"status":1}]',
        otherSetting: null,
        systemVersion: 'target_hwhx2.1.2423.139 2024-06-07 19:36:56',
    },
};

let sanitizedResponse2222 = sanitizeResponse(mockResponse);
console.log(sanitizedResponse2222);
